package com.hzdl.apple.controller.homeCotroll;

import java.io.IOException;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.hzdl.apple.bean.User;
import com.hzdl.apple.service.UserService;



@WebServlet("/user_login")
public class LoginController extends HttpServlet{

	private UserService service;
	
	@Override
	public void init(ServletConfig config) throws ServletException {
		super.init(config);
		service = new UserService();
	}
	
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		String loginId = req.getParameter("loginId");
		String loginPsw = req.getParameter("loginPsw");
		User user = service.login(loginId, loginPsw);
		if (user == null) {
			req.setAttribute("errMsg", "用户名或密码错误！");
			//重新登录
			req.getRequestDispatcher("/home/login").forward(req, resp);
		}else{//登录成功！
			req.getSession().setAttribute("user", user);
			
			req.setAttribute("success", "登陆成功");
			req.getRequestDispatcher("/home/index").forward(req, resp);
			
		}
	}
	
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		doGet(req, resp);
	}
}
